<template>
	<om-formsub-outer :index="index" @save="save" @delete="del">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {subOnLoad, formValidation, changeEditor, configSelectData} from '../../../static/js/formUtil.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return {
				formdata:{},
				initValue:{
					targetValueCode:"",
					totalNum:'',
					ratioName:''
				},
				autoformdata: [],
				cachename:'',
				index:null,
				datas:[],
				order:0
			}
		},
		methods: {
			save(){
				let _that = this
				var checkRes = formValidation(this.$refs.autoform.configData, this.formdata)
				if (!checkRes) {
					if(this.index != null){
						Object.assign(this.datas[this.index],this.formdata)
					}else{
						this.datas.push(this.formdata)
					}
					uni.setStorage({
						key: this.cachename,
						data: this.datas,
					})
					uni.navigateBack()
				} else {
					_that.om.toast(checkRes);
				}
			},
			del() {
				if(this.index != null) {
					this.datas.splice(this.index,1)
				}
				uni.setStorage({
					 key: this.cachename,
					 data: this.datas
				})
				uni.navigateBack()
			},
			onSelectIndex(item){
				this.$refs.autoform.formdata.orderNum = item.orderNum;
				this.$refs.autoform.formdata.targetValueName = item.targetValueName;
				this.$refs.autoform.formdata.targetValueCode = item.targetValueCode;
				this.$refs.autoform.formdata.totalNum = item.totalNum;
				this.$refs.autoform.formdata.ratioName = item.ratioName;
			},
			initConfig(e){
				let _that = this
				_that.om.request("tweekMeetingReport/getCurrentWeekIndexList",{
					zposId: e.zposId,
					year: e.year,
					week: e.week
				}).then(res=>{
					//if(res instanceof Array){
					if(Object.prototype.toString.call(res)==='[object Array]'){
						_that.$refs.autoform.changeEditor("title7","indexId","data",configSelectData(res,"indexId","indexName"))
						_that.$refs.autoform.initSelectIndex("title7","indexId")
					}
				})
			}
		},
		onLoad(e){
			let _that = this
			subOnLoad(_that, e, function() {
				if(e.configDataCacheKey){
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if(res.data && res.data instanceof Array){
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								_that.initConfig(e)
							}
						}
					});
				}
			})
		}
	}
</script>

<style>

</style>
